﻿@{
    ViewBag.Title = "Select";
}

<h2>List of Selected Courses</h2>
@if (ViewBag.SelectedCourses == null || ViewBag.SelectedCourses.Count == 0)
{
    <h4>"There are no courses. Please, use the <a href='/Courses/Index'>Courses</a> page to search for courses."</h4>
    return;
}


<table>
        @if (ViewBag.SelectedCourses != null && ViewBag.SelectedCourses.Count!=0)
        {
            <tr><th>Number</th><th>Name</th><th>Points</th><th>Scores</th>
            </tr>
            ViewBag.Points = 0;
            foreach (string c in ViewBag.SelectedCourses.Keys)
                    {
                        <tr>
                            <td><a href="/Courses/Info/?number=@c">@c</a></td>
                            <td>@(Server.UrlDecode(ViewBag.SelectedCourses[c]).Split('$')[0])</td>                            
                            <td id='@("points" + c)'>                                
                                @if(Server.UrlDecode(ViewBag.SelectedCourses[c]).Split('$').Length == 2)
                                {
                                    ViewBag.Points = (double)ViewBag.Points + Convert.ToDouble(Server.UrlDecode(ViewBag.SelectedCourses[c]).Split('$')[1]);
                                    @(Server.UrlDecode(ViewBag.SelectedCourses[c]).Split('$')[1])
                                    
                                }
                            </td>
                            <td>
                                <input id='@("score" + c)' value="100" onkeydown="currentValue=this.value;" 
                                    onkeyup="@("grade" + c)=this.value; calculateAverage();"/>
                            </td>
                            <td><a href='@("/Courses/Unselect/?number=" + c)'>Unselect</a></td>
                        </tr>
                    }
        }
    <tr><td></td><td></td><td>@ViewBag.Points</td><td id="average">Average: 100</td></tr>
    <tr><td></td><td></td><td></td><td><a href="#" onclick="lucky();">I'm Feeling Lucky!</a></td></tr>

</table>

<br />
 <h5>Now you can choose your schedule! <a href="/scheduler">Go!</a> </h5>


<script type="text/javascript">
    @foreach (string c in ViewBag.SelectedCourses.Keys)
    {
        @("var grade" + c + "=100;\n")
        @("var point" + c + "=")
        if(Server.UrlDecode(ViewBag.SelectedCourses[c]).Split('$').Length == 2)
        {
            @(Server.UrlDecode(ViewBag.SelectedCourses[c]).Split('$')[1] + ";\n")
        }else{
            @:"0;\n"  
        }
    }
    var average = 100;
    function calculateAverage() {
        average = (0 @foreach (string c in ViewBag.SelectedCourses.Keys)
                    {
                        @(" + grade" + c + "*point" + c);    
                    })/@ViewBag.Points;
        document.getElementById('average').innerHTML = 'Average: ' + average.toString();
    }

    function lucky() {
        @foreach (string c in ViewBag.SelectedCourses.Keys)
        {
            <text>
            @("grade" + c + "=Math.floor((Math.random()*100)+1);\n")
            @Html.Raw("document.getElementById('score" + c + "').value=grade" + c + ".toString();\n")
            </text>
        }
        calculateAverage();
    }
</script>
